package com.tianqu.tms.www.cardect.controller;

import java.util.List;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.DectCodes;
import com.tianqu.tms.entity.TmsDetectPic;
import com.tianqu.tms.www.cardect.viewhelper.CarNormalDetectViewHelper;

/***
 * 
 * 待检测（人工）基本检测报告录入首页Controller
 * 
 * @author tianshifu
 * 
 */
public class CarWDPersonNormalDetectTopController extends TmsController {
	private static final String DETECT_INPUT_DATA = "detect_input_data";// 检测录入信息
	private static final String IMGLIST = "imgList";// 用于存放图片对象的list

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			// 因为每次录入完之后都需要走这个查询的Action，所以先判断uc中是否有值
			CarNormalDetectViewHelper view = new CarNormalDetectViewHelper();
			// 每次返回的时候都从uc中取值set到viewhelp中
			if (uc.getAttribute(DETECT_INPUT_DATA) != null) {
				view = (CarNormalDetectViewHelper) uc
						.getAttribute(DETECT_INPUT_DATA);
			}

			// 将图片信息set到viewhelp中
			// 上传图片部分，判断某部位是否有图片
			List<TmsDetectPic> list = (List<TmsDetectPic>) uc
					.getAttribute(IMGLIST);
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					TmsDetectPic entity = (TmsDetectPic) list.get(i);
					// 从这里获取图片信息放到viewhelp中
					if (entity.getImgType() == 1) {
						if (entity.getImgKey().equals("1")) {
							view.setImg01(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("2")) {
							view.setImg02(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("3")) {
							view.setImg03(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("4")) {
							view.setImg04(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("5")) {
							view.setImg05(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("6")) {
							view.setImg06(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("7")) {
							view.setImg07(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("8")) {
							view.setImg08(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
						if (entity.getImgKey().equals("9")) {
							view.setImg09(entity.getImgPath());
							view
									.setImgType(String.valueOf(entity
											.getImgType()));
						}
					}
				}
			} else {
				view.setImg01("");
				view.setImg02("");
				view.setImg03("");
				view.setImg04("");
				view.setImg05("");
				view.setImg06("");
				view.setImg07("");
				view.setImg08("");
				view.setImg09("");
				view.setImgType("1");
			}

			DectCodes code = DectCodes.getInstance();
			// 返回的时候需要将八大配置转成code,把默认值改成汉字，每次进来的时候都转一下
			if (view.getSkylight().equals("有")
					|| view.getSkylight().equals("无")) {
				view.setSkylight(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getSkylight()));
			}
			if (view.getLeatherSeats().equals("有")
					|| view.getLeatherSeats().equals("无")) {
				view.setLeatherSeats(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getLeatherSeats()));
			}
			if (view.getAluminumRim().equals("有")
					|| view.getAluminumRim().equals("无")) {
				view.setAluminumRim(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getAluminumRim()));
			}
			if (view.getGpsNavigation().equals("有")
					|| view.getGpsNavigation().equals("无")) {
				view.setGpsNavigation(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getGpsNavigation()));
			}
			if (view.getAutoWindow().equals("有")
					|| view.getAutoWindow().equals("无")) {
				view.setAutoWindow(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getAutoWindow()));
			}
			if (view.getSrsNumber().equals("有")
					|| view.getSrsNumber().equals("无")) {
				view.setAbs(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view.getAbs()));
			}
			if (view.getFourWheelDrive().equals("有")
					|| view.getFourWheelDrive().equals("无")) {
				view.setSrsNumber(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getSrsNumber()));
			}
			if (view.getAbs().equals("有") || view.getAbs().equals("无")) {
				view.setFourWheelDrive(code.getItemCodeByItemIdAndItemValue(
						DectCodes.EIGHT_VEHICLE_CONFIGURATION, view
								.getFourWheelDrive()));
			}

			if (view.getDisasterCars().equals("正常")
					|| view.getDisasterCars().equals("水淹")
					|| view.getDisasterCars().equals("火烧")
					|| view.getDisasterCars().equals("盐害")) {
				view.setDisasterCars(code.getItemCodeByItemIdAndItemValue(
						DectCodes.TRAFFIC_CAR, view.getDisasterCars()));
			}

			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}
}
